package org.easy.bochaai;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.easy.bochaai.dto.BochaBaseModel;
import org.easy.bochaai.dto.WebSearchRequest;
import org.easy.bochaai.dto.WebSearchResponse;
import org.easy.core.util.HttpUtil;

import java.util.HashMap;


/**
 * https://aq6ky2b8nql.feishu.cn/wiki/RXEOw02rFiwzGSkd9mUcqoeAnNK
 */
public class BochaWebSearchClient {


    private final static String REQUEST_URL_TEMPLATE = "https://api.bochaai.com/v1/web-search";

    private String apiKey;

    public BochaWebSearchClient(String key) {
        this.apiKey = key;
    }


    /**
     * @param query
     * @param summary
     * @param freshness oneDay  oneWeek oneMonth oneYear noLimit
     * @param count
     * @return
     */
    public BochaBaseModel<WebSearchResponse> webSearch(String query, boolean summary, String freshness, Integer count) {
        WebSearchRequest webSearchRequest = new WebSearchRequest();
        webSearchRequest.setQuery(query);
        webSearchRequest.setSummary(summary);
        webSearchRequest.setFreshness(freshness);
        webSearchRequest.setCount(count);

        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer " + apiKey);

        String response = HttpUtil.postJson(REQUEST_URL_TEMPLATE, headers, webSearchRequest);
        return JSON.parseObject(response, new TypeReference<BochaBaseModel<WebSearchResponse>>() {
        });

    }


}